/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hawk.demo;

import glz.hawkgenerator.java.ClassSpec;
import glz.hawkgenerator.java.ConstructorSpec;
import glz.hawkgenerator.java.EnumSpec;
import glz.hawkgenerator.java.MethodSpec;
import hawk.util.ProjectUtils;

import javax.lang.model.element.Modifier;

import static glz.hawkgenerator.java.type.PrimitiveTypeName.INT;

/**
 * 枚举类生成示例
 *
 * @author Hawk
 */
public class EnumGenerator {
    private final String packageName;

    public EnumGenerator(String packageName) {
        this.packageName = packageName;
    }

    /**
     * 生成最简单的枚举类，仅仅包含枚举量和其对应的javadoc
     */
    public void generateSimpleEnum() {
        EnumSpec enumSpec = EnumSpec.builder("Speed", Modifier.PUBLIC)
                .addEnumConstant("SLOW", ClassSpec.anonymousBuilder().setJavadoc("speed < 50 km/s").build())
                .addEnumConstant("MEDIUM", ClassSpec.anonymousBuilder().setJavadoc("speed >= 50 km/s & speed < 00 km/s").build())
                .addEnumConstant("FAST", ClassSpec.anonymousBuilder().setJavadoc("speed >= 50 km/s & speed < 00 km/s").build())
                .build();
        ProjectUtils.write(packageName, enumSpec);
    }

    /**
     * 生成的枚举类，包含枚举量，枚举量赋值，构造函数，公共方法
     * <p>每一个枚举常量本质上是一个匿名类</p>
     */
    public void generateComplexEnum() {
        EnumSpec enumSpec = EnumSpec.builder("Priority", Modifier.PUBLIC)
                .addEnumConstant("HIGHEST", "$L, $S", 9, "highest")
                .addEnumConstant("HIGH", "$L, $S", 7, "high")
                .addEnumConstant("MEDIUM", "$L, $S", 5, "medium")
                .addEnumConstant("LOW", "$L, $S", 3, "low")
                .addEnumConstant("LOWEST", "$L, $S", 1, "lowest")
                .addEnumConstant("OTHER", ClassSpec.anonymousBuilder("$L, $S", 0, "other").setJavadoc("Other").build())
                .addField(String.class, "name", Modifier.FINAL)
                .addField(int.class, "level", Modifier.FINAL)
                .addConstructor(ConstructorSpec.builder()
                        .addParameter(INT, "level")
                        .addParameter(String.class, "name")
                        .beginConstructorBody()
                        .addStatement("this.name = name")
                        .addStatement("this.level = level")
                        .end()
                        .build())
                .addMethod(MethodSpec.builder(String.class, "getName")
                        .beginMethodBody()
                        .addStatement("return name")
                        .end().build())
                .addMethod(MethodSpec.builder(INT, "getLevel")
                        .beginMethodBody()
                        .addStatement("return level")
                        .end().build())
                .build();
        ProjectUtils.write(packageName, enumSpec);
    }

    /**
     * 生成枚举类，包含抽象方法，构造函数
     */
    public void generateSpecialComplexEnum() {
        EnumSpec enumSpec = EnumSpec.builder("Shape", Modifier.PUBLIC)
                .addEnumConstant("TRIANGLE",
                        ClassSpec.anonymousBuilder("$S", "triangle")
                                .setJavadoc("The sum of the interior angles of a triangle is 180 degrees")
                                .addMethod(MethodSpec.builder(INT, "sidesCount", Modifier.PUBLIC)
                                        .beginMethodBody()
                                        .addStatement("return 3")
                                        .end().build())
                                .build())
                .addEnumConstant("RECTANGLE",
                        ClassSpec.anonymousBuilder("$S", "rectangle")
                                .setJavadoc("The sum of the interior angles of a rectangle is 360 degrees")
                                .addMethod(MethodSpec.builder(INT, "sidesCount", Modifier.PUBLIC)
                                        .beginMethodBody()
                                        .addStatement("return 4")
                                        .end().build())
                                .build())
                .addField(String.class, "name", Modifier.FINAL)
                .addConstructor(ConstructorSpec.builder()
                        .addParameter(String.class, "name")
                        .beginConstructorBody()
                        .addStatement("this.name = name")
                        .end()
                        .build())
                .addMethod(MethodSpec.builder(String.class, "getName", Modifier.PUBLIC)
                        .beginMethodBody()
                        .addStatement("return name")
                        .end()
                        .build())
                .addMethod(MethodSpec.builder(INT, "sidesCount", Modifier.PUBLIC, Modifier.ABSTRACT).build())
                .build();
        ProjectUtils.write(packageName, enumSpec);
    }
}
